<?php

namespace teach\service;

use Yii;

class Users
{
    /**
     * 获取一条产品信息
     * @param $pid
     * @param $sku_id
     * @return array|false
     */
    public function productOne($pid, $sku_id)
    {
        $sql = "SELECT p.id, p.mid, p.img, p.title, p.group_num, p.old_price, s.assemble_price FROM mall_assemble_product AS p LEFT JOIN mall_assemble_specs AS s ON p.id = s.pid WHERE p.id = {$pid} AND s.id = {$sku_id}";;
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 获取拼团组
     * @param $group_id
     * @return array
     */
    public function orderGroup($group_id)
    {
        $sql = "SELECT id, avatar_url, nickname, mode_id FROM mall_assemble_order WHERE group_id = '{$group_id}'";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }

    public function orders($where, $page, $pre_page = 10, $state)
    {
        $count = $this->orderCount($where);
        $data = ['total' => 0, 'total_page' => 0, 'page' => 1, 'pre_page' => $pre_page, 'lists' => []];

        if ($count) {
            $total_page = ceil( $count / $pre_page );
            $offset = ( $page - 1 ) * $pre_page;
            $sql = "SELECT id,mid, pid,out_trade_no, total_fee, logistic_fee, name, phone, address, order_num, count, create_time, avatar_url, status, group_id, mode_id FROM mall_assemble_order WHERE {$where} ORDER BY create_time DESC LIMIT {$offset}, {$pre_page}";
            $res = Yii::$app->db->createCommand($sql)->queryAll();
            $resData = [];
            foreach ($res as $_key => $_val) {
                $productSql = "SELECT id, img, old_price, group_num FROM mall_assemble_product WHERE {$_val['pid']}";
                $product = Yii::$app->db->createCommand($productSql)->queryOne();
                //$_val['pid'] = $product['id'];
                $_val['img'] = $product['img'];
                $_val['old_price'] = $product['old_price'];

                // 统计订单是否成团
                $numSql = "SELECT COUNT(*) FROM mall_assemble_order WHERE group_id = '{$_val['group_id']}'";
                $group_num = Yii::$app->db->createCommand($numSql)->queryScalar();
                $_val['group'] = $product['group_num'] - $group_num;

                //如果state = 6 把status 改成 11
                if ($state == 6) {
                    $_val['status'] = 11;
                }
                $resData[$_key] = $_val;
            }
            $data = ['total' => $count, 'total_page' => $total_page, 'page' => $page, 'pre_page' => $pre_page, 'lists' => $resData];
        }
        return $data;
    }

    private function orderCount($where)
    {
        $sql = "SELECT COUNT(*) FROM mall_assemble_order WHERE {$where}";
        return Yii::$app->db->createCommand($sql)->queryScalar();
    }

    /**
     * 获取一条订单记录
     * @param $param
     * @return array|false
     */
    public function orderOne($param)
    {
        $sql = "SELECT * FROM mall_assemble_order WHERE {$param}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 修改订单状态
     * @param $map
     * @param $param
     * @return int
     */
    public function orderStatus($map, $param)
    {
        $sql = "UPDATE mall_assemble_order SET {$map} WHERE {$param}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 拼团人数加一
     * @param $map
     * @param $where
     * @return int
     */
    public function assembleAdd($map, $where)
    {
        $sql  = "UPDATE mall_assemble_product SET {$map} WHERE {$where} ";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 获取一条商家用户信息
     * @param $where
     * @return array|false
     */
    public function userShop($where)
    {
        $sql = "SELECT openid, id,nickname FROM edu_user WHERE {$where}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 获取产品信息
     * @param $pid
     * @return array|false
     */
    public function productAssembleOne($pid)
    {
        $sql = "SELECT id, sub, status FROM mall_assemble_product WHERE id = {$pid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }


}